package com.pt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pt.entity.Request;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

@Mapper
public interface RequestMapper extends BaseMapper<Request> {
    
    /**
     * 分页查询求种列表，包含关联信息
     */
    @Select("<script>" +
            "SELECT r.*, " +
            "u1.username as requester_username, " +
            "u2.username as filler_username, " +
            "t.id as torrent_id, " +
            "t.title as torrent_title, " +
            "t.size as torrent_size, " +
            "t.info_hash as torrent_info_hash, " +
            "CASE " +
            "  WHEN r.category_id = 1 THEN '电影' " +
            "  WHEN r.category_id = 2 THEN '电视剧' " +
            "  WHEN r.category_id = 3 THEN '音乐' " +
            "  WHEN r.category_id = 4 THEN '软件' " +
            "  WHEN r.category_id = 5 THEN '游戏' " +
            "  WHEN r.category_id = 6 THEN '电子书' " +
            "  WHEN r.category_id = 7 THEN '动漫' " +
            "  WHEN r.category_id = 8 THEN '其他' " +
            "  ELSE '未分类' " +
            "END as category_name " +
            "FROM request r " +
            "LEFT JOIN user u1 ON r.requester_id = u1.id " +
            "LEFT JOIN user u2 ON r.filler_id = u2.id " +
            "LEFT JOIN torrent t ON r.filled_torrent_id = t.id " +
            "WHERE 1=1 " +
            "<if test='categoryId != null'> AND r.category_id = #{categoryId} </if>" +
            "<if test='status != null'> AND r.status = #{status} </if>" +
            "ORDER BY r.created_time DESC" +
            "</script>")
    IPage<Map<String, Object>> selectRequestsWithInfo(Page<?> page, @Param("categoryId") Integer categoryId, @Param("status") Integer status);
    
    /**
     * 根据ID查询求种详情，包含关联信息
     */
    @Select("SELECT r.*, " +
            "u1.username as requester_username, " +
            "u2.username as filler_username, " +
            "t.id as torrent_id, " +
            "t.title as torrent_title, " +
            "t.size as torrent_size, " +
            "t.info_hash as torrent_info_hash, " +
            "CASE " +
            "  WHEN r.category_id = 1 THEN '电影' " +
            "  WHEN r.category_id = 2 THEN '电视剧' " +
            "  WHEN r.category_id = 3 THEN '音乐' " +
            "  WHEN r.category_id = 4 THEN '软件' " +
            "  WHEN r.category_id = 5 THEN '游戏' " +
            "  WHEN r.category_id = 6 THEN '电子书' " +
            "  WHEN r.category_id = 7 THEN '动漫' " +
            "  WHEN r.category_id = 8 THEN '其他' " +
            "  ELSE '未分类' " +
            "END as category_name " +
            "FROM request r " +
            "LEFT JOIN user u1 ON r.requester_id = u1.id " +
            "LEFT JOIN user u2 ON r.filler_id = u2.id " +
            "LEFT JOIN torrent t ON r.filled_torrent_id = t.id " +
            "WHERE r.id = #{requestId}")
    Map<String, Object> selectRequestWithInfoById(@Param("requestId") Long requestId);
} 